package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Description:
 * 定时任务类
 * @author Yanxinhao on 2024/11/20
 */
@Component
public class ScheduledTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    //定时检查租约状态是否过期，如果过期那么将status设置为过期
    //检查时间，每一天的0点检查
    @Scheduled(cron = "0 0 0 * * *")
    public void updateLeaseAgreementStatus(){
        UpdateWrapper<LeaseAgreement> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .le(LeaseAgreement::getLeaseEndDate,new Date())
                .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING)
                .set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(updateWrapper);
    }
}